package com.yuanhua.sim.netty.utils;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;


import java.time.LocalDateTime;

/**
 * channel绑定熟悉操作
 * @author yuanhua
 */
@Slf4j
public class NettyAttrUtil {

    private static final AttributeKey<String> ATTR_KEY_READ_TIME = AttributeKey.valueOf("last.read.time");

    public static void updateReaderTime(Channel channel, Long time){
        log.info("更新最近读事件时间");
        channel.attr(ATTR_KEY_READ_TIME).set(time.toString());
    }

    public static Long getReaderTime(Channel channel) {
        String value = getAttribute(channel, ATTR_KEY_READ_TIME);
        if (value != null){
            return Long.valueOf(value);
        }
        return null;
    }

    private static String getAttribute(Channel channel, AttributeKey<String> attrKeyReadTime) {
        return channel.attr(attrKeyReadTime).get();
    }

}
